﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ivy.Common.Framework.GeneralUtility;
using Ivy.UserRegistration.IBusinessLogic;
using Ivy.UserRegistration.BusinessLogic;
using Ivy.UserRegistration.Entity;
using HSMPWeb.ViewEntity;

namespace HSMPWeb
{
	public partial class Site : System.Web.UI.MasterPage
    {
        #region  Variable& constant
        #endregion  Variable& constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMenu();
            }

            if (Session["UserContext"] != null)
            {
                UserContext UserSession = (UserContext)(Session["UserContext"]);
                lblUser.Text = UserSession.CurrentUser.UserFirstName.ToString() + " " + UserSession.CurrentUser.UserLastName.ToString();
                //login.Visible = false;
                //logout.Visible = true;
                Page.RegisterStartupScript("myScript", "<script language=JavaScript>$('#login').hide(); $('#logout').show();</script>");
            }
            else
            {
                lblUser.Text = string.Empty;
                //login.Visible = true;
                //logout.Visible = false;
                Page.RegisterStartupScript("myScript", "<script language=JavaScript>$('#login').show(); $('#logout').hide();</script>");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text.Trim() != "")
            {
                Response.Redirect("https://www.google.co.in/search?q=" + txtSearch.Text.Trim(), false);
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                IUserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();

                if (lUserRegistrationBus.Login(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
                {
                    User lUser = new User();
                    lUser = lUserRegistrationBus.GetUserByLoginID(txtUserName.Text.Trim());
                    UserContext UserSession = new UserContext();
                    //UserSession.UserLoginID = lUser.UserLoginID;
                    //UserSession.UserFirstName = lUser.UserFirstName;
                    //UserSession.UserLastName = lUser.UserLastName;
                    //UserSession.UserPrimaryEmailID = lUser.UserPrimaryEmailID;
                    //UserSession.UserVerificationCode = lUser.UserVerificationCode;
                    //UserSession.UserIsVerified = lUser.UserIsVerified;
                    UserSession.CurrentUser = lUser;
                    Session["UserContext"] = UserSession;
                    if (lUser.UserIsVerified)
                    {
                        // Response.Redirect("~/Default.aspx");
                        lblUser.Text = UserSession.CurrentUser.UserLoginID;
                        //login.Visible = false;
                        //logout.Visible = true;
                        //Page.RegisterStartupScript("myScript", "<script language=JavaScript>$('#logout').show(); $('#login').hide();</script>");
                        Response.Redirect(Request.Url.AbsolutePath);
                    }
                    else
                    {
                        Response.Redirect("~/Pages/UserVerification.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            lblUser.Text = string.Empty;
            //Page.RegisterStartupScript("myScript", "<script language=JavaScript>$('#login').show(); $('#logout').hide();</script>");
            Response.Redirect(Request.Url.AbsolutePath);
        }
        #endregion Events

        #region Private Methods
        private void LoadMenu()
        {
            smoothmenu1.Items.Add(new MenuItem("Home", "", "", "~/HsmpHomePage.aspx"));
            MenuItem mItemsMasterData = new MenuItem("Master Data", "");
            mItemsMasterData.ChildItems.Add(new MenuItem("Society Master", "", "", "~/Pages/SocietyDetails.aspx"));
            mItemsMasterData.ChildItems.Add(new MenuItem("Flat Master", "", "", "~/Pages/FlatDetails.aspx"));
            smoothmenu1.Items.Add(mItemsMasterData);
            MenuItem mItems = new MenuItem("Products", "");
            mItems.ChildItems.Add(new MenuItem("Feedback Service", "", "", "~/Pages/ComingSoon.aspx?PageHeadline=Feedback Service – Coming soon"));
            smoothmenu1.Items.Add(mItems);
            smoothmenu1.Items.Add(new MenuItem("Forum", "", "", "~/Pages/ComingSoon.aspx?PageHeadline="));
            smoothmenu1.Items.Add(new MenuItem("Feedback", "", "", "~/Pages/FeedbackList.aspx"));
            smoothmenu1.Items.Add(new MenuItem("About Us", "", "", "~/About.aspx"));
        }
        #endregion Private Methods
        
	}
}